package com.hua.common.commontools;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.io.IOException;

/**
 * Created by huaxiaolin on 2016/4/9.
 */
public class FileUtil {

    /**
     * @return true 存在、false 不存在
     * @throws
     * @Methods: isSdCardExist
     * @Description: 检测SD卡是否存在
     */
    public static boolean isSdCardExist() {
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean createFile(String Path, String name) throws IOException {
        boolean flog = false;
        if (isSdCardExist()) {
            File folder  = new File(Path);
            if (!folder.exists()) {
                folder.mkdirs();
            }
            File myFile = new File(folder.getAbsolutePath(), name);
            if (myFile.exists()) {
                myFile.delete();
            }
            MyLog.writeSystemLog("huaxiaolin      "+myFile.getAbsolutePath());
            flog = myFile.createNewFile();

        }
        return flog;
    }



    /**
     * sd卡可用空间
     *
     * @return sd卡总空间
     */
    public static long getAvailaleSize() {
        File path = Environment.getExternalStorageDirectory(); // 取得sdcard文件路径
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }


    /**
     * 获取应用根目录
     *
     * @param con 上下文
     * @return /data/data/com.greenpoint.joycity/
     */
    public static String getPath(Context con) {
        String path = con.getFilesDir().getParent();
        return path + "/";
    }

    /**
     * 得到SD卡的目录
     *
     * @return /mnt/sdcard/JOY_CITY/
     */
    public static String getSDCardPath() {
        return Environment.getExternalStorageDirectory() + "/JOY_CITY/";
    }
}
